<?php
class comment extends top
{
	//重定义构造函数
    function __construct(){  
        parent::__construct();
        //定义当前导航
        $this->nav_name = 'comment';
        $this->page_title = '留言管理';
        $this->breadcrumb += array('1'=>array('name'=>'留言','url'=>spUrl('comment')));

    }
	function index(){	
		$data = $this->spArgs();

		$left_nav_result[] = array(
				'id'=>0,
				'name'=>'反馈留言',
				'url'=>spUrl('comment','index',array('module'=>'feedback','module_id'=>0)),
		);
		$this->left_nav_result = $left_nav_result;
		
		$this->left_nav_id = 0;
		$module = 'feedback';
		$this->module = $module;
		$module_id = 0;
		$this->module_id =  $module_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>'反馈留言','url'=>spUrl('comment','index',array('module'=>'feedback','module_id'=>0))),
        			'3'=>array('name'=>'列表')
        		);

		//成功添加、更新链接后的提示
		$notice = array($data['state'],$data['message']);
		
		$comment = spClass("m_comment");
		//删除
		if($data['delete_comment_id']){
			if($comment->delete('id='.$data['delete_comment_id'])){
				$notice = array('success','删除成功！');
			}else{
				$notice = array('danger','删除出错！');
			}
		}	
		$this->notice = $notice[0]?$notice:null;
		
		//查找		
		if($data['s_submit']){
			$where = "( other_content like '%" .$data['s_content']. "%' OR content like '%" .$data['s_content']. "%' ) ";
			$where .= " AND module='".$module."'";
			$where .= " AND module_id='".$module_id."'";
		}else{
			$where .= "module='".$module."'";
			$where .= " AND module_id='".$module_id."'";
		}
		$sort= 'time DESC';
		$comment_result = $comment->spPager($this->spArgs('page', 1), 10)->findAll($where,$sort);
		$this->pager = $comment->spPager()->getPager();
		foreach ($comment_result as $key => $value) {
			$comment_result[$key]['other_item_arr'] = explode(',', $value['other_item']);
			$comment_result[$key]['other_content_arr'] = explode(',', $value['other_content']);
		}
		$this->comment_result = $comment_result;

		$this->c = 'comment';
		$this->a = 'index';		
		$this->data_return = $data;		
	}
	function opera(){
		$data = $this->spArgs();
		//获取当前分类id
		$cat_id = isset($data['cat_id'])?$data['cat_id']:0;
		//构造左侧导航
		$cat_id = $this->__left_nav($cat_id);
		
		if(!is_numeric($cat_id)) $this->__yt_error();
		$this->left_nav_id = $cat_id;
		$this->cat_id =  $cat_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>$cat_info['name'],'url'=>spUrl('url','index',array('cat_id'=>$cat_info['id']))),
					'3'=>array('name'=>"添加")
        		);

		//默认为添加链接
		$data_return = array('update_add'=>"添加",'cat_id'=>$cat_id);
		//更新链接
		$url = spClass("m_url");
		if(is_numeric($data['update_url_id'])){	
			$data_return = $url->find(array('id'=>$data['update_url_id']));
			if(empty($data_return)) $this->__yt_error();
			$data_return['update_add'] = "更新";
			$breadcrumb = $this->breadcrumb;
			$breadcrumb['3'] = array('name'=>$data_return['title']);
			$breadcrumb['4'] = array('name'=>"更新");
			$this->breadcrumb = $breadcrumb;
		}
		//提交链接
		if($data['submit']){
			if(1){
				$data_insert = array(
					'editor_id'=>$_SESSION['ID'],
					'cat_id'=>$data['cat_id'],
					'title'=>$data['title'],
					'img_dir'=>$data['img_dir']?$data['img_dir']:0,
					'content'=>$data['content'],
					'description'=>$data['description'],
				);

				if($data['id']){
					$is_ok = $url->update('id='.$data['id'],$data_insert);
				}else{
					$is_ok = $url->create($data_insert);
				}
				if($is_ok){
					$this->jump(spUrl('url', 'index',array('cat_id'=>$data['cat_id'],'state'=>'success','message'=>$data['update_add'].'成功！','content_only'=>$this->content_only))); 
				}else{
					$data_return = $data;
					$this->notice = array('danger',$data['update_add'].'出错！');
				}
			}else{
				$data_return = $data;
				$this->notice = array('danger','填写有误！');				
			}
		}
		$this->data_return = $data_return;
	}	
}